package com.zdlog.wenjuan.controller.android;

import com.zdlog.wenjuan.VO.ResultBoolenVO;
import com.zdlog.wenjuan.dataobject.FormInfo;
import com.zdlog.wenjuan.dataobject.ResultInfo;
import com.zdlog.wenjuan.dataobject.SubmitInfo;
import com.zdlog.wenjuan.dto.android.FormDTO;
import com.zdlog.wenjuan.service.FormService;
import com.zdlog.wenjuan.service.SubmitService;
import com.zdlog.wenjuan.service.WebSocket;
import com.zdlog.wenjuan.util.DateFormatHelper;
import com.zdlog.wenjuan.util.FileUtil;
import com.zdlog.wenjuan.util.ResultBoolenVOUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @program: wenjuanSer
 * @PACKAGE_NAME: com.zdlog.wenjuan.controller.android
 * @author: Kuaile
 * @create: 2018-02-11 20:19
 * android端请求
 **/
@RestController
@RequestMapping("/android/form")
@Slf4j
public class FormController {

    @Autowired
    private FormService formService;

    @Autowired
    private SubmitService submitService;

    @Autowired
    private WebSocket webSocket;



    @GetMapping("/find")
    public List<FormDTO> list(@RequestParam(value = "name" ,defaultValue = "") String name) {
        List<FormDTO> formDTOList = new ArrayList<>();
        List<FormInfo> formInfo = formService.findName(name, new Integer(1));
        FormDTO formDTO = null;
        if (formInfo.size() != 0) {
            for (FormInfo formInfo1 : formInfo) {
                formDTO = new FormDTO();
                BeanUtils.copyProperties(formInfo1, formDTO);
                formDTOList.add(formDTO);
            }

        }
        return formDTOList;
    }

    @GetMapping("/getForm")
    public FormDTO getForm(@RequestParam(value = "id") String id) {
        return formService.findById(id);
    }

    @PostMapping("/submitForm")
    public ResultBoolenVO submitForm(@RequestParam(value = "formId") String formId,
                                     @RequestParam(value = "submitId") String submitId,
                                     @RequestParam(value = "startTime") String startTime,
                                     @RequestParam(value = "endTime") String endTime,
                                     @RequestParam(value = "address") String address,
                                     @RequestParam(value = "problem") String problem) {

        SubmitInfo submitInfo = new SubmitInfo();
        submitInfo.setSubmitId(submitId);
        submitInfo.setAddress(address);
        submitInfo.setFormId(formId);
        submitInfo.setStartTime(DateFormatHelper.StringToData(startTime, DateFormatHelper.DateFormat.DATE_8));
        submitInfo.setEndTime(DateFormatHelper.StringToData(endTime, DateFormatHelper.DateFormat.DATE_8));
        List<ResultInfo> resultInfoList = new ArrayList<>();
        ResultInfo resultInfo;
        JSONArray jsonArray = JSONArray.fromObject(problem);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Set set = jsonObject.keySet();
            for (Object key : set) {
                resultInfo = new ResultInfo();
                resultInfo.setProblemId((String) key);
                resultInfo.setResult(jsonObject.getString((String) key));
                resultInfo.setSubmitId(submitId);
                resultInfoList.add(resultInfo);
            }

        }
        submitService.submitForm(submitInfo, resultInfoList);
        FormInfo formInfo = formService.findId(formId);
        webSocket.sendMessage("新问卷", formInfo.getUserId());
        return ResultBoolenVOUtil.success();
    }


    @PostMapping("/uploadImage")
    public ResultBoolenVO uploadImage(@RequestParam("file") MultipartFile file,@RequestParam(value = "submitId") String submitId, @RequestParam(value = "problemId") String problemId){
        String contentType = file.getContentType();
        //文件名+扩展名
        String fileName = problemId+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        /*System.out.println("fileName-->" + fileName);
        System.out.println("getContentType-->" + contentType);*/
        String filePath = "src/main/resources/static/imgupload/" + submitId + "/";
        try {
            FileUtil.uploadFile(file.getBytes(), filePath, fileName);
        } catch (Exception e) {
            // TODO: handle exception
        }
        //返回json
        return ResultBoolenVOUtil.success();
    }



}
